﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;

namespace Dmo_lc_2.Shape
{
    public class MLeaderShape : BaseShape
    {
        public MTextShape mText; // 标注文字
        public int contentType; // 标注类型  2 多行文字标注


        public double textHeight; // 文字高度
        public bool enableFrameText;
        public double[] blockScale;
        public double blockRotation;
        public int[] blockColor;

        /**
         *         AttachmentHorizontal = 0,
         *         AttachmentVertical = 1
         */
        public int textAttachmentDirection; // 文字方向

        public int[] textColor; // 文字颜色

        /** 文字对齐
         *          LeftAlignment = 0,
         *           CenterAlignment = 1,
         *           RightAlignment = 2
         */
        public int textAlignmentType;

        /*
         *         InsertAngle = 0,
         *         HorizontalAngle = 1,
         *         AlwaysRightReadingAngle = 2
         */
        public int textAngleType;
        public int textAttachmentType;

        public int leaderCount; // 多重引线个数
        public int leaderLineCount; // 引线个数
        public int leaderLineType;

        public int[] leaderLineColor; // 引线颜色
        public int leaderLineWeight;  // 引线宽

        public bool extendLeaderToText;
        public double landingGap;
        public bool enableDogleg;
        public double doglegLength;

        public double arrowSize;

        public bool enableLanding;
        public double scale;

        public double[] blockPosition;
        
        //        public List<Dictionary<string, object>> leaders;
        public List<LeaderItem> leaders = new List<LeaderItem>();
        public List<BaseShape> arrowShaps = new List<BaseShape>();


        public MLeaderShape(Entity in_entity, Transaction tr) : base(in_entity, tr)
        {
        }

        public int weight { get; set; }

        public override void Init()
        {
            MLeader in_leader = (MLeader)mEntity;

            ObjectId lineTypeId = in_leader.LinetypeId;
            shapeType = ShapeType.MLEADER;
            shapeTypeName = shapeType.ToString();

            MText mt = in_leader.MText;
            BaseShape bs = ShapeFactory.genShapeByEntity(mTransaction, mt);
            if (bs != null && bs.shapeType == BaseShape.ShapeType.MTEXT)
                mText = (MTextShape)bs;
            
            contentType = (int)in_leader.ContentType;
            textHeight = in_leader.TextHeight;
            enableFrameText = in_leader.EnableFrameText;
            blockScale = new double[] { in_leader.BlockScale.X, in_leader.BlockScale.Y,in_leader.BlockScale.Z };
            blockRotation = in_leader.BlockRotation;
            blockColor = new int[] { in_leader.BlockColor.ColorValue.R ,
                in_leader.BlockColor.ColorValue.G, in_leader.BlockColor.ColorValue.B };

            textAttachmentDirection = (int)in_leader.TextAttachmentDirection;
            textColor = new int[] { in_leader.TextColor.ColorValue.R ,
                in_leader.TextColor.ColorValue.G, in_leader.TextColor.ColorValue.B };
            textAlignmentType = (int)in_leader.TextAlignmentType;

            textAngleType = (int)in_leader.TextAngleType;
            textAttachmentType = (int)in_leader.TextAttachmentType;
            leaderCount = (int)in_leader.LeaderCount;
            leaderLineCount = (int)in_leader.LeaderLineCount;
            leaderLineType = (int)in_leader.LeaderLineType;

            leaderLineColor = new int[] { in_leader.LeaderLineColor.ColorValue.R ,
                in_leader.LeaderLineColor.ColorValue.G, in_leader.LeaderLineColor.ColorValue.B };

            leaderLineWeight = (int)in_leader.LeaderLineWeight;

            extendLeaderToText = in_leader.ExtendLeaderToText;
            landingGap = in_leader.LandingGap;
            enableDogleg = in_leader.EnableDogleg;
            doglegLength = in_leader.DoglegLength;
            arrowSize = in_leader.ArrowSize;
            enableLanding = in_leader.EnableLanding;
            scale = in_leader.Scale;

            ArrayList leaderIndexs = in_leader.GetLeaderIndexes();
            for (int i = 0; i < leaderCount; i++) {
                LeaderItem tempLeaderItem = new LeaderItem();
                tempLeaderItem.leaderIndex = (int)leaderIndexs[i];
                Vector3d tempDogleg = in_leader.GetDogleg((int)leaderIndexs[i]);
                tempLeaderItem.dogleg = new double[] { tempDogleg.X, tempDogleg.Y, tempDogleg.Z };
                tempLeaderItem.doglegLength = in_leader.GetDoglegLength((int)leaderIndexs[i]);


                ArrayList leaderLineIndexs = in_leader.GetLeaderLineIndexes((int)leaderIndexs[i]);
                for (int j = 0; j < leaderLineIndexs.Count; j++) {
                    int tempLineIndex = (int)leaderLineIndexs[j];
                    LeaderLineItem tempLeaderLineItem = new LeaderLineItem();
                    tempLeaderLineItem.arrowSize = in_leader.GetArrowSize(tempLineIndex);

                    Point3d tempFirst = in_leader.GetFirstVertex(tempLineIndex);
                    tempLeaderLineItem.firstVertex = new double[] {
                        tempFirst.X,tempFirst.Y,tempFirst.Z  } ;

                    Point3d tempLast = in_leader.GetLastVertex(tempLineIndex);
                    tempLeaderLineItem.lastVertex = new double[] {
                        tempLast.X,tempLast.Y,tempLast.Z  };

                    Color tempLineColor = in_leader.GetLeaderLineColor(tempLineIndex);
                    tempLeaderLineItem.leaderLineColor = new int[] {
                        tempLineColor.ColorValue.R,tempLineColor.ColorValue.G,tempLineColor.ColorValue.B  };

                    tempLeaderLineItem.leaderLineType = (int)in_leader.GetLeaderLineType(tempLineIndex);
                    tempLeaderLineItem.leaderLineWeight = (int)in_leader.GetLeaderLineWeight(tempLineIndex);
                    
                    int verticesCount = in_leader.VerticesCount(tempLineIndex);
                    for (int m = 0; m < verticesCount; m++) {

                        Point3d temppos = in_leader.GetVertex(tempLineIndex , m);
                        double[] tempVertice = new double[] { temppos.X, temppos.Y, temppos.Z };
                        tempLeaderLineItem.vertices.Add(tempVertice);
                    }

                    tempLeaderItem.leaderLines.Add(tempLeaderLineItem);

                }

                leaders.Add(tempLeaderItem);
            }
            int lineSize = leaders.Count;
            string dimBlk1Name = "default";
            ObjectId dimBlk1 = in_leader.ArrowSymbolId;

            arrowShaps = getDimBlkShapesByObjId(dimBlk1, mTransaction, ref dimBlk1Name);

        }

        private List<BaseShape> getDimBlkShapesByObjId(ObjectId inId, Transaction inTr, ref String dimBlkStr)
        {
            if (inId.IsNull)
            {
                dimBlkStr = "default";
                return null;
            }

            BlockTableRecord dimBlk1Recorder = (BlockTableRecord)inTr.GetObject(inId, OpenMode.ForRead);
            List<BaseShape> result = new List<BaseShape>();

            dimBlkStr = dimBlk1Recorder.Name;
            foreach (ObjectId oid in dimBlk1Recorder)
            {
                DBObject dbobj = mTransaction.GetObject(oid, OpenMode.ForRead);
                if (dbobj is Entity)
                {
                    Entity entity = dbobj as Entity;
                    BaseShape bs = ShapeFactory.genShapeByEntity(mTransaction, dbobj);
                    if (bs != null && bs.shapeType != BaseShape.ShapeType.BASE)
                    {
                        bs.color = leaderLineColor;

                    }
                    result.Add(bs);
                }
            }

            return result;
        }
    }

    public class LeaderItem {
        public List<LeaderLineItem> leaderLines = new List<LeaderLineItem>();
        public int leaderIndex;
        public double[] dogleg;
        public double doglegLength;
    }

    public class LeaderLineItem
    {
        public double arrowSize;
        public double[] firstVertex;
        public double[] lastVertex;
        public int[] leaderLineColor;

        public int leaderLineType;
        public int leaderLineWeight;

        public List<double[]> vertices = new List<double[]>();

    }
}
 